﻿/**
 * ...
 * @author George Mavchun
 * @author Артём Григорьев
 * @author Alex Malygin
 * 
 */

package gui.hug;

import client.INews;
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
import gui.hug.priv.HUGTabButton;
import list.FavoritesEvent;
import list.NewList;
import loader.Loader;
import tab.FavoritesTab;
import client.Channel;
import tab.MainTab;

class HUGTabbedPanel extends HUGSprite
{
	private var tabsCount : Int;
	private var RightTabsCount : Int;
	private var LeftTabsCount : Int;
	
	private var tabs : Array<Tab>;
	private var RightInvTabs: Array<Tab>;
	private var LeftInvTabs: Array<Tab>;
	//private var tabs : Array<HUGSprite>;
	//private var tabNames : Array<String>;
	//private var buttonsArray : Array<HUGTabButton>;
	
	private var button : HUGTabButton;
	
	private var closeButton : HUGTabButton;
	private var miniButton : HUGTabButton;
		
	//private var currentSprite : HUGSprite;
		
	private var maxTabHeight : Float;
	private var maxTabWidth : Float;
	private var ToRight:Button;
	private var ToLeft:Button;
	private var ToBeg:Button;
	private var Temp:Button;
	private var Temp1:Button;
	private var TL:Button;
	private var TC:Button;
	private var rs:String;
	private var ls:String;
	private var tem:String;
	private var tem1:String;
	private var tl:String;
	private var tc:String;
	public var selectedTab : Int;
	public var previousTab : Int;
	public var FavClick:Bool;
	public var load: Loader;
	public var FavArr: Array<Channel>;

	public function new(width : Float = 0) 
	{
		super();
		tabsCount = 0;
		RightTabsCount = 0;
		LeftTabsCount = 0;
		//tabs = new Array<HUGSprite>();
		//tabNames = new Array<String>();
		//buttonsArray = new Array<HUGTabButton>();
		
		tabs = new Array<Tab>();
		RightInvTabs = new Array<Tab>();
		LeftInvTabs = new Array<Tab>();
		
		maxTabHeight = 0;
		maxTabWidth = 0;
		maxTabWidth = width;
		FavClick = false;
	}
	
	public function draw() 
	{
		if (selectedTab == 0) selectedTab = 1;
		
		
				
		var active : Bool;
		var x : Float = 60;
		for (i in 1...(tabsCount+1)){
			x += 6;
			active = (i == selectedTab) ? true : false;
			button = new HUGTabButton(tabs[i].getName(), active);
			button.setLocation(x, 0);
			button.addEventListener(MouseEvent.CLICK, tabButtonClick);
			button.name = Std.string(i);
			//buttonsArray[i] = button;
			tabs[i].setButton(button);
			if(600 > button.x + button.width){
			    addChild(button);
		        x += button.getFactWidth(); 
			}
			else {
				var cur:Tab = tabs.pop();
				//RightInvTabs.push(cur);
				RightInvTabs.insert(0, cur);
				
                RightTabsCount++;
				tabsCount--;
			}
		}
		
		x += 12;
				
		if (x < maxTabWidth) {
			x = maxTabWidth;
		}
		if (LeftTabsCount > 0) { 
			ToLeft = new  LightButton("<-",33,0,25,HUGColors.VK_BORDER_INNER,HUGColors.VK_BORDER_INNER,12,1,0,11,4);
			ToBeg = new LightButton("<<<", 0, 0, 30, HUGColors.VK_BORDER_INNER, HUGColors.VK_BORDER_INNER, 12, 1, 0, 11, 4 );
			}
		else {
			ToLeft = new  LightButton("<-", 33, 0, 25, HUGColors.VK_BACKGROUND, HUGColors.VK_BORDER_INNER, 12, 1, 0, 11, 4);	
			ToBeg = new LightButton("<<<", 0, 0, 30, HUGColors.VK_BACKGROUND, HUGColors.VK_BORDER_INNER, 12, 1, 0, 11, 4 );
		}
		if (RightTabsCount > 0) 
		ToRight = new LightButton("->", 600, 0, 25, HUGColors.VK_BORDER_INNER, HUGColors.VK_BORDER_INNER, 8, 1, 0, 11, 4);
		else 
		ToRight = new LightButton("->", 600, 0, 25, HUGColors.VK_BACKGROUND, HUGColors.VK_BORDER_INNER, 8, 1, 0, 11, 4);
		drawLine(x);
		drawButtons();
		
		drawBackGround(x);
		
		var currentSprite = tabs[selectedTab].getContent();
		currentSprite.setLocation(0, 25);
		addChild(currentSprite);
		addChild(ToRight);
		ToRight.addEventListener(MouseEvent.CLICK, MoveToLeft);
		addChild(ToLeft);  
		ToLeft.addEventListener(MouseEvent.CLICK, MoveToRight);
		addChild(ToBeg);
		ToBeg.addEventListener(MouseEvent.CLICK, MoveToBeg);
	}
	
	private function drawButtons()
	{
		var delta : Float = 0;
		if ((tabs[selectedTab] == null) && (tabs[selectedTab - 1] != null)) 
			selectedTab += - 1 ;
		else {
				if ((tabs[selectedTab] == null) && (tabs[selectedTab - 1] == null)) selectedTab = 1;
				}
			
		if (tabs[selectedTab].isClosable()) {
			closeButton = new HUGTabButton("x", false);
			delta += closeButton.getFactWidth();
			closeButton.setLocation(maxTabWidth - delta, 0);
			closeButton.addEventListener(MouseEvent.CLICK, closeTab);
			addChild(closeButton);
		}
		
		//miniButton = new HUGTabButton("v", false);
		//delta += miniButton.getFactWidth() + 2;
		//miniButton.setLocation(maxTabWidth - delta, 0);
		//addChild(miniButton);
	}
	
	private function reDrawButtons()
	{
		if (closeButton != null && this.contains(closeButton)) {
			removeChild(closeButton);
		}
		
		//if (miniButton != null && this.contains(miniButton)) {
		//	removeChild(miniButton);
		//}
		
		drawButtons();
	}
	
	private function drawLine(width : Float) 
	{
		var line : Shape = new Shape();
		line.graphics.lineStyle(1, HUGColors.VK_DELIMETER_LINE);
		line.graphics.moveTo(0, 0);
		line.graphics.lineTo(width, 0);
		line.y = 24;
		addChild(line);
	}
	
	private function drawBackGround(width : Float) 
	{
		var background : Shape = new Shape();
		background.graphics.beginFill(HUGColors.VK_BACKGROUND);
		background.graphics.drawRect(0, 0, width, maxTabHeight);
		background.graphics.endFill();
		background.y = 21;
		addChild(background);
	}
	
	private function tabButtonClick(e : Event) 
	{
		//trace(e.target.name);		
		
		setTab(Std.parseInt(e.target.name));
	}
	
	public function setTab(e : Int) 
	{
		previousTab = selectedTab;
		selectedTab = e;

		for (i in 1...(tabsCount + 1)) {
			if (i == e) {
				tabs[i].getButton().setState(true);
			} else {
				tabs[i].getButton().setState(false);
			}
		}
		
		dispatchEvent(new Event(Event.CHANGE));
		
		// changing main content
		var currentSprite = tabs[previousTab].getContent();
		removeChild(currentSprite);		
		
		//var currentSprite = tabs[selectedTab].getContent();
		if (tabs[selectedTab].getName() == "Избранное")
		{
			FavClick = true;
			var favtab:FavoritesTab = cast(tabs[selectedTab], FavoritesTab);
			currentSprite = cast(tabs[selectedTab], Tab).getContent();
			favtab.Refresh();
		}
			if (tabs[selectedTab].getName() == "Главная")
		{
			var maintab:MainTab = cast(tabs[selectedTab], MainTab);
			currentSprite = cast(tabs[selectedTab], Tab).getContent();
			maintab.Refresh();
		}
		else {currentSprite = tabs[selectedTab].getContent();}
		currentSprite.setLocation(0, 25);
		addChild(currentSprite);
		/*if (tabs[selectedTab].getName() == "Избранное")
		{
			cast(tabs[selectedTab], FavoritesTab).getNewContent();
			currentSprite = cast(tabs[selectedTab], Tab).getContent();
			trace("-------------------------FavoritesTab------------------");
		}
		else {currentSprite = tabs[selectedTab].getContent();}*/
		
		// managing buttons (tab may be unclosable!)
		reDrawButtons();
	}
	
	public function MoveToRight(e: MouseEvent){
		if ((LeftTabsCount > 0) && (LeftInvTabs.length > 0 )) {
		RightTabsCount++;
		var right: Tab = tabs.pop();
		//RightInvTabs.push(right);
		RightInvTabs.insert(0, right);
		
		var curr: Tab = LeftInvTabs.pop();
		tabs.insert(1, curr); 
		LeftTabsCount--;
		reDraw();
		}		
	}
	public function MoveToLeft(e: MouseEvent){
		if ((RightTabsCount > 0) && (RightInvTabs.length > 0 )) {
		LeftTabsCount++;
		LeftInvTabs.push(tabs[1]);
		tabs.remove(tabs[1]);
		var curr1: Tab = RightInvTabs[0];
		RightInvTabs.remove(curr1);
		tabs.push(curr1);		
	    RightTabsCount--;
		reDraw();
	    }
	}
	public function MoveToBeg(e: MouseEvent) {
	    while (LeftTabsCount > 0) {
		    RightTabsCount++;
		    var right: Tab = tabs.pop();
		    //RightInvTabs.push(right);
			RightInvTabs.insert(0, right);
			
		    var curr: Tab = LeftInvTabs.pop();
		    tabs.insert(1, curr); 
		    LeftTabsCount--;	
		}
		reDraw();
	}
	/**
	 * @return	index of the tab if it exists in tabs set
	 * @return	-1 if the tab does not exusts in tabs set
	 * @param	tab
	 */
	public function getIndex(tab : Tab) : UInt
	{
		for (i in 1 ... (tabsCount + 1)) {
			var t = tabs[i];
			if (t.equals(tab)) return i;
		}
		
		return -1;
	}
	
	/**
	 * adds new tab into tabs set
	 * if it already exists this method just makes it shown
	 * @param	tab
	 */
	public function addTab(tab : Tab) 
	{		
		//var index = getIndex(tab);
		//if (index != -1) {
		//	setTab(index);
		//	return;
		//}
		var content = tab.getContent();
		tabsCount++;
		//tabs[tabsCount] = tab;
		
		
		if (content.height + content.y > maxTabHeight) {
			maxTabHeight = content.height + content.y;
		}
		if (content.width + content.x > maxTabWidth) {
			maxTabWidth = content.width + content.x;
		}
		//if (content.x+content.width > 0) { var cur = content.getChildByName("knopka");
		//if(cur != null)
		//content.removeChildAt(4);
		//}
		tab.setContent(content);
		tabs[tabsCount] = tab;
		reDraw();
		setTab(tabsCount);
	}
	
	public function getSelectedTabName() 
	{
		return tabs[selectedTab].getName();
	}
	
	
	private function clearLayout() 
	{
		while (numChildren > 0) {
			removeChildAt(numChildren - 1);
		}
	}
	
	private function reDraw() 
	{
		clearLayout();
		draw();
	}
	
	private function closeTab(_) 
	{
		//var tmpTabs = new Array<HUGSprite>();
		//var tmpTabNames = new Array<String>();
		//var tmpButtons = new Array<HUGTabButton>();
		
		var tmpTabs = new Array<Tab>();
		tmpTabs = tabs;
	    tmpTabs.remove(tmpTabs[selectedTab]);
		var j = 1;
		
	//	for (i in 1...(tabsCount + 1)) {
	//		if (selectedTab != i) {
				//tmpTabs[j] = tabs[i];
				//tmpTabNames[j] = tabNames[i];
				//tmpButtons[j] = buttonsArray[i];				
				
		//		tmpTabs[j] = tabs[i];
				
		//		j++;
		//	}
		//}
		
		//tabs = tmpTabs;
		//tabNames = tmpTabNames;
		//buttonsArray = tmpButtons;
		//currentSprite = (previousTab < selectedTab) ?
		//					tabs[previousTab] :
		//					tabs[previousTab - 1];
		if (RightInvTabs.length>0 && RightTabsCount>0) {
		    //var curr1: Tab=RightInvTabs.pop();
			var curr1:Tab = RightInvTabs[0];
			RightInvTabs.remove(curr1);
			
		    tmpTabs.push(curr1);		
	        RightTabsCount--;
			tabsCount++;
		}
		else if ((LeftTabsCount > 0) && (LeftInvTabs.length > 0 )) {
		    var curr: Tab = LeftInvTabs.pop();
		    tabs.insert(1, curr); 
		    LeftTabsCount--;
			tabsCount++;
		}
		
		tabs = tmpTabs;
				
		tabsCount--;
		
		selectedTab = previousTab;
		
		reDraw();
	}
	public function GetClick():Bool {  return FavClick;	}
	
		public function onLoader(data : Array<Channel>) : Void
	{
		for (channel in data)
		{   
			FavArr.push(channel);
		}
	}


}